/**
 * Created by Administrator on 2017/1/10.
 */

var GlobalConfig = require('../../config/config.json');
var mongodb = require("mongodb");

module.exports = {
    instance: null,
    plat: null,
    user: null,
    world: null,

    stat: null,
    pay: null,
    log: null,
    feedback: null,
    onlinepay: null,
    createrooms:null,
    match: null,

    task: null,

    // 加载数据库
    loadDB: function(callback) {
        var mongoServer = new mongodb.Server(GlobalConfig.db.host, GlobalConfig.db.port,
            {auto_reconnect: true, poolSize: 4});
        var db = new mongodb.Db(GlobalConfig.db.name, mongoServer,
            {'native_parser': false, 'w': 1, 'wtimeout': 10, 'fsync': true});

        db.open(function(err, db) {
            if (err || !db) {
                callback(err || "db invalid"); return;
            }

            this.instance = db;
            this.plat = db.collection("plat");
            this.user = db.collection("user");
            this.world = db.collection("world");
            this.stat = db.collection("stat");
            this.pay = db.collection("pay");
            this.log = db.collection("log");
            this.feedback = db.collection("feedback");
            this.onlinepay = db.collection("onlinepay");
            this.match = db.collection("match");

            this.task = db.collection("task");
            callback();
        }.bind(this));
    },

    initDB: function(callback) {
        var db = this.instance;

        async.series([
            function(cb) {
                db.createCollection('plat', {}, function(err, result) {
                    var dbPlat = db.collection('plat');
                    dbPlat.insertOne({_id:'_userid', 'ai': GlobalConfig.logic.minUid}, function(err, result) {
                        cb();
                    });
                });
            },
            function(cb) {
                db.createCollection('user', {}, function(err, result) {
                    cb();
                });
            },
            function(cb) {
                db.createCollection('world', {}, function(err, result) {
                    var dbWorld = db.collection("world");
                    // 保存已经使用的房间号
                    dbWorld.insertOne({_id:'usedRoomIds', 'ids': {}}, function(err, result){
                        cb();
                    });
                });
            },
            function(cb) {
                db.createCollection('stat', {}, function(err, result) {
                    cb();
                });
            },
            function(cb) {
                db.createCollection('pay', {}, function(err, result) {
                    cb();
                });
            },
            function(cb) {
                db.createCollection('log', {}, function(err, result) {
                    cb();
                });
            },
            function(cb) {
                db.createCollection('feedback', {}, function(err, result) {
                    cb();
                });
            },
            function (cb) {
                db.createCollection('onlinepay', {}, function(err, result) {
                    cb();
                });
            },
            function (cb) {
                db.createCollection('createrooms', {}, function(err, result) {
                    cb();
                });
            },
            function (cb) {
                db.createCollection('match', {}, function(err, result) {
                    cb();
                });
            },
            function (cb) {
                db.createCollection('task', {}, function(err, result) {
                    cb();
                });
            },
        ], function(err) {
            if (!err) {
                this.plat = db.collection("plat");
                this.user = db.collection("user");
                this.world = db.collection("world");
                this.stat = db.collection("stat");
                this.pay = db.collection("pay");
                this.log = db.collection("log");
                this.feedback = db.collection("feedback");
                this.onlinepay = db.collection("onlinepay");
                this.createrooms = db.collection("createrooms");
                this.match = db.collection("match");
                this.task = db.collection("task");
            }
            callback(err);
        }.bind(this));
    },
}

